package service;

import java.util.List;

import javax.persistence.EntityManager;

import model.User;
import util.EMUtil;

public class UserService extends BaseService<User> {
	/**
	 */
	private static final long serialVersionUID = 1L;
	EntityManager em = EMUtil.getEM();
	public void add(User user){
		save(user);
	}
	
	public void delete(User u) {
		remove(u);
	}
	
	@SuppressWarnings("unchecked")
	public User find(User user){
		String sql = "select * from user where 1=1";
		if(user.getId() != null){
			sql = "select * from user where id="+user.getId();
		}else if(user.getUsername() != null)
			sql += " and username='"+user.getUsername()+"' ";
		List<User> list = em.createNativeQuery(sql, User.class).getResultList();
		if(list.size()>0)
			user = list.get(0);
		else
			user = null;
		return user;
	}

	public List<User> getList() {
		
		@SuppressWarnings("unchecked")
		List<User> userList = em.createNativeQuery("select * from user",User.class).getResultList();
		
		return userList;
	}

	
}
